﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar.Controls;
using BLL;

namespace IPR_TrainExamSys_TY.Employee
{
    public partial class FrmEmployeeSet : Form
    {
        string sMark = "";
        DataTable dtCur = new DataTable();
        string sLabelName="";
        string sTxtName="";
        string sTxtValue = "";
        public FrmEmployeeSet(DataTable dt,string mark)
        {
            InitializeComponent();
            sMark = mark;
            dtCur = dt;
            if (mark == "A")
            {
                lbTitle.Text = "员工信息增加";
            }
            if (mark == "U")
            {
                lbTitle.Text = "员工信息修改";
                try
                {
                    for (int i = 0; i < dtCur.Columns.Count; i++)
                    {
                        setControlValue(dtCur.Columns[i].Caption, dtCur.Rows[0][dtCur.Columns[i].Caption].ToString());
                        //Control[] con = panelEx1.Controls.Find(dtCur.Columns[i].Caption, true);
                        //if (con.Length > 0)
                        //{
                        //    TextBoxX tb = con[0] as TextBoxX;
                        //    tb.Text = dtCur.Rows[0][dtCur.Columns[i].Caption].ToString();
                        //}
                    }
                }
                catch (Exception exc)
                {
                }
            }
            
        }

        private void FrmEmployeeSet_Load(object sender, EventArgs e)
        {
            getLocomotiveNo();
            //getWorkShop("");
            //getClassGroup("");
        }

        private void getLocomotiveNo()
        {
            try
            {
                string sWhere = " IsDelete=0 and DepartMentLevel=0 ";
                DataSet ds = new T_PublicDepartment().GetList(sWhere);
                if(ds!=null)
                    if(ds.Tables.Count>0)
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            LocomotiveNo.DataSource = ds.Tables[0];
                            LocomotiveNo.DisplayMember = "DepartmentName";
                            LocomotiveNo.ValueMember = "DepartmentId";
                        }
            }
            catch
            {
            }
        }
        private void getWorkShop(string DepartMentPID)
        {
            try
            {
                WorkShop.Items.Clear();
                string sWhere = " IsDelete=0 and DepartMentLevel=1 ";
                if (DepartMentPID != "")
                    sWhere += " and DepartMentPID= " + DepartMentPID;
                DataSet ds = new T_PublicDepartment().GetList(sWhere);
                if (ds != null)
                    if (ds.Tables.Count > 0)
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            WorkShop.DataSource = ds.Tables[0];
                            WorkShop.DisplayMember = "DepartmentName";
                            WorkShop.ValueMember = "DepartmentId";
                        }
            }
            catch
            {
            }
        }
        private void getClassGroup(string DepartMentPID)
        {
            try
            {
                ClassGroup.Items.Clear();
                string sWhere = " IsDelete=0 and DepartMentLevel=2 ";
                if (DepartMentPID != "")
                    sWhere += " and DepartMentPID= " + DepartMentPID;
                DataSet ds = new T_PublicDepartment().GetList(sWhere);
                if (ds != null)
                    if (ds.Tables.Count > 0)
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            ClassGroup.DataSource = ds.Tables[0];
                            ClassGroup.DisplayMember = "DepartmentName";
                            ClassGroup.ValueMember = "DepartmentId";
                        }
            }
            catch
            {
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            Model.T_PublicEmployee mpemployee = InitModelEmplwy();
            if (sMark == "U")
            {
                new T_PublicEmployee().Update(mpemployee);
                MessageBox.Show("数据更新完成", "更新完成");
            }
            else if (sMark == "A")
            {
                if (EmployeeId.Text.Trim() == "")
                {
                    MessageBox.Show("请输入工号", "数据不完整");
                    return;
                }
                if (GWHGZSBH.Text.Trim() == "")
                {
                    MessageBox.Show("请输入岗位合格证书编号", "数据不完整");
                    return;
                }
                string sWhere = " EmployeeId='" + EmployeeId.Text.Trim() + "' or GWHGZSBH='"+GWHGZSBH.Text.Trim()+"'";
                Model.T_PublicEmployee selmPe= new T_PublicEmployee().GetModel(sWhere);
                if (selmPe != null)
                {
                    MessageBox.Show("工号或者岗位合格证书编号重复！", "员工信息重复");
                    return;
                }
                long lr= new T_PublicEmployee().Add(mpemployee);
                if (lr > 0)
                    MessageBox.Show("数据添加完成", "添加完成");
            }
        }
        private void setControlValue(string conName, string conText)
        {
            object conValue = new object();
            Control[] con = panelEx1.Controls.Find(conName, true);
            if (con.Length > 0)
            {
                Control findCon = con[0];
                Type conType = findCon.GetType();
                if (conType.Name == "TextBoxX")
                {
                    TextBoxX tb = findCon as TextBoxX;
                    tb.Text = conText; 
                }
                else if (conType.Name == "DateTimePicker")
                {
                    DateTimePicker tb = findCon as DateTimePicker;
                    if(conText!="")
                        try
                        {
                            tb.Value = Convert.ToDateTime(conText);
                        }
                        catch
                        {
                        }
                }
                else if (conType.Name == "ComboBox")
                {
                    ComboBox tb = findCon as ComboBox;
                    tb.Text = conText;
                }
                else if (conType.Name == "ComboBoxEx")
                {
                    ComboBox tb = findCon as ComboBox;
                    tb.Text = conText;
                }
            }
        }
        private object getControlValue(string conName)
        {
            object conValue = new object();
            Control[] con = panelEx1.Controls.Find(conName, true);
            if (con.Length > 0)
            {
                Control findCon = con[0];
                Type conType = findCon.GetType();
                if (conType.Name == "TextBoxX")
                {
                    TextBoxX tb = findCon as TextBoxX;
                    conValue = tb.Text.Trim();
                }
                else if (conType.Name == "DateTimePicker")
                {
                    DateTimePicker tb = findCon as DateTimePicker;
                    conValue = tb.Value.ToString("yyyy-MM-dd");
                }
                else if (conType.Name == "ComboBox")
                {
                    ComboBox tb = findCon as ComboBox;
                    conValue = tb.Text.Trim();
                }
                else if (conType.Name == "ComboBoxEx")
                {
                    ComboBox tb = findCon as ComboBox;
                    conValue = tb.Text.Trim();
                }
            }
            else conValue = null;
            return conValue;
        }
        private Model.T_PublicEmployee InitModelEmplwy()
        {
            Model.T_PublicEmployee mPEmployee = new Model.T_PublicEmployee();
            try
            {
                Type type = mPEmployee.GetType();
                System.Reflection.PropertyInfo[] properInfo = type.GetProperties();
                for (int i = 0; i < properInfo.Length; i++)
                {
                    System.Reflection.PropertyInfo pi = type.GetProperty(properInfo[i].Name);
                    //if(i==2) pi.SetValue(mPEmployee, "222222",null);//设置model类的值
                    //object val = pi.GetValue(mPEmployee, null);
                    //string value = val == null ? "" : val.ToString(); 
                    //sb += pi.Name + "|" + value + "_"; //获取值 

                    object value = new object();
                    object conValue = getControlValue(pi.Name);

                    string p_type = pi.PropertyType.ToString();
                    if (p_type.Contains("decimal"))
                    {
                        if (conValue != null)
                            value = Convert.ToDecimal(conValue);
                        else value = null;
                    }
                    else if (p_type.Contains("Int32"))
                    {
                        if (conValue != null)
                            value = Convert.ToInt32(conValue);
                        else value = null;
                    }
                    else if (p_type.Contains("Int64"))
                    {
                        if (conValue != null)
                            value = Convert.ToInt64(conValue);
                        else value = null;
                    }
                    else if (p_type.Contains("Datetime") || p_type.Contains("date"))
                    {
                        if (conValue != null)
                            value = Convert.ToDateTime(conValue);
                        else value = null;
                    }
                    else if (p_type.Contains("String"))
                    {
                        if (conValue != null)
                            value = Convert.ToString(conValue);
                        else value = null;
                    }
                    else if (p_type.Contains("long"))
                    {
                        if (conValue != null)
                            value = Convert.ToUInt64(conValue);
                        else value = null;
                    }
                    else if (p_type.ToLower().Contains("byte"))
                    {
                        if (conValue != null)
                            value = System.Text.Encoding.Default.GetBytes(conValue.ToString());
                        else value = null;
                    }
                    else if (p_type.ToLower().Contains("bool"))
                    {
                        if (conValue != null)
                            value = conValue.ToString() == "是" ? true : false;
                        else value = null;
                    }
                    if (value != null)
                    {
                        pi.SetValue(mPEmployee, value, null);
                    }

                }
                return mPEmployee;

            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message + "\r\n","读取值错误" );
                return null;
            }
        }

        private void cmbWorkShop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (WorkShop.SelectedValue == null) return;
            getClassGroup(WorkShop.SelectedValue.ToString());
        }

        private void cmbLocomotiveNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LocomotiveNo.SelectedValue == null) return;
            getWorkShop(LocomotiveNo.SelectedValue.ToString());
        }
    }
}
